Week 10: Mapping

Intro to dataset: Mass Shootings in the US

library(rworldmap)
library(ggmap)


msaData <- read_csv("Stanford_MSA_Database.csv")

head(msaData)
msaData %>% dplyr::select(-Description, 
                          -'Day of Week', 
                          -'Shooter Name', 
                          -'Type of Gun - Detailed', 
                          -'Targeted Victim/s - Detailed', 
                          -'Possible Motive - Detailed', 
                          -'History of Mental Illness - Detailed', 
                          -starts_with("Data "), -Notes) -> msaDataTrunc

head(msaDataTrunc)

Scatterplots on Maps

us <- c(left = -126, bottom = 24.5, right = -66, top = 50)
myMap <- get_stamenmap(us, zoom=5, maptype="toner-lite")



ggmap(myMap) +
  labs(x="Longitude", 
       y="Latitude", 
       title="Heat Map of US Mass Shooting Fatalities")+
  geom_point(data = msaDataTrunc, aes(x=Longitude, y=Latitude), alpha=.5, size=1)

Variation - Size by Fatalities

us <- c(left = -126, bottom = 24.5, right = -66, top = 50)
myMap <- get_stamenmap(us, zoom=5, maptype="watercolor")


ggmap(myMap) +
  labs(x="Longitude", 
       y="Latitude", 
       title="Heat Map of US Mass Shooting Fatalities")+
  geom_point(data = msaDataTrunc, aes(x=Longitude, y=Latitude), alpha=.5, size=msaDataTrunc$`Number of Civilian Fatalities`)

Heat Map

us <- c(left = -119.5, bottom = 33, right = -116, top = 34.5)
myMap <- get_stamenmap(us, zoom=8, maptype="terrain")



ggmap(myMap) +
  geom_density2d(data=msaDataTrunc, aes(x=Longitude, y=Latitude), size=0.3)+
  stat_density2d(data=msaDataTrunc, 
                 aes(x=Longitude, y=Latitude, fill=..level.., alpha=..level..),
                 size=0.01, bins=20,
                    geom="polygon") +
  scale_fill_gradient(low = "blue", high = "orange") +
  scale_alpha(range = c(0, 0.3), guide = FALSE)+
  labs(x="Longitude", y="Latitude", title="Heat Map of US Mass Shooting Fatalities")+
  geom_point(data = msaDataTrunc, aes(x=Longitude, y=Latitude), alpha=.5, size=1)

Annotations (Text, shapes, etc)

us <- c(left = -126, bottom = 24.5, right = -66, top = 50)
myMap <- get_stamenmap(us, zoom=5, maptype="terrain")

ggmap(myMap) +
  geom_density2d(data=msaDataTrunc, aes(x=Longitude, y=Latitude), size=0.3)+
  stat_density2d(data=msaDataTrunc, 
                 aes(x=Longitude, y=Latitude, fill=..level.., alpha=..level..),
                 size=0.01, bins=10,
                    geom="polygon") +
  scale_fill_gradient(low = "blue", high = "orange") +
  scale_alpha(range = c(0, 0.3), guide = FALSE)+
  labs(x="Longitude", y="Latitude", title="Heat Map of US Mass Shooting Fatalities")+
  geom_point(data = msaDataTrunc, aes(x=Longitude, y=Latitude), alpha=.5, size=1)+
  annotate('rect', 
           xmin=-83.5, 
           ymin=39.5, 
           xmax=-82, 
           ymax=40.5, 
           col="red", 
           fill="transparent")+
  annotate("text", 
           x=-83, 
           y=39, 
           label="Columbus, OH", 
           col="red" ,
           size=3)

Arrows

garlicData <- read_csv("GarlicMustardData.csv")

head(garlicData)
us <- c(left = -126, bottom = 24.5, right = -66, top = 50)
myMap <- get_stamenmap(us, zoom=5, maptype="terrain")

ggmap(myMap)+
  geom_point(data = garlicData, aes(x=Longitude,  y=Latitude), 
             alpha=.5, size=garlicData$AvgAdultHeight/20)+
  annotate("segment", 
           x=-88, 
           y=35, 
           xend=-83, 
           yend = 40, 
           color="red", 
           arrow=arrow(length=unit(.3,"cm")))

Cloropleth Maps

states <- map_data(map="state")

#note: mapping algs are very sensitive to variable names

#in our dataset, it's called "State".  BUT, we need it to be "region"

msaDataTrunc %>% rename(region = State) -> msaDataTrunc

library(openintro)     ## students will need to install this library

msaDataTrunc$region %>% 
  tolower() ->
  msaDataTrunc$region

states <- left_join(states, count(msaDataTrunc, region))  #joining map data with shooting data

head(states)
#Note:  need to determine color on the map.  More shootings means brighter color!

ggplot() +
  geom_map(data=states, 
           map=states, 
           aes(map_id=region, x=long, y=lat, fill=n)) +
  scale_fill_gradient(low="purple", high="orange", guide="colourbar")+
  labs(x = "Longitude", y="Latitude", title ="Cloropleth Map of Mass Shootings")